package com.netsmart.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netsmart.bean.Admin;
import com.netsmart.bean.AdminSentence;
import com.netsmart.bean.sentence;
import com.netsmart.dao.AdminMapper;
import com.netsmart.dao.AdminSentenceMapper;
import com.netsmart.dao.sentenceMapper;
import com.netsmart.dto.base.BaseDto;
import com.netsmart.dto.base.ResponseDate;
import com.netsmart.enums.HttpEnum;
import com.netsmart.enums.StateEnum;
import com.netsmart.service.LoginService;
import com.netsmart.util.MD5;


@Service
public class LoginServiceImpl extends BaseDto implements LoginService {
    @Autowired
    AdminMapper adminDao;

    @Autowired
    AdminSentenceMapper asDao;
    @Autowired
    sentenceMapper sDao;
    
    
    
    @Override
    public String login(String json) {
        try {
            Map<String, String> map = gson.fromJson(json, HashMap.class);
            String field2 = map.get("phone");
            String adminPass = map.get("adminPass");
            if (field2 == null || adminPass == null) {
                return gson.toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.FAIL.getState(), "手机号或密码为空"));
            } else {
                Admin admin = adminDao.selectByPhone(field2);
                if (admin != null && admin.getAdminPass() != null
                        && adminPass.equals(admin.getAdminPass())) {
                    return gson.toJson(new ResponseDate<Admin>(HttpEnum.OK.getState(), StateEnum.SUCCESS.getState(), "登录成功",admin));
                } else {
                    return gson
                            .toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.FAIL.getState(), "手机号或密码错误"));
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return gson.toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.INNER_ERROR.getState(), "网络异常"));
        }
    }
    
    
    @Override
    public String register(String json) {
        try {
            Map<String, String> map = gson.fromJson(json, HashMap.class);
            String phone = map.get("phone");
            String adminName = map.get("adminName");
            String adminPass = map.get("adminPass");
            if (phone == null || "".equals(phone) || adminPass == null||"".equals(adminPass)||adminName==null||"".equals(adminName)) {
                return gson.toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.FAIL.getState(), "手机号、用户名或密码为空"));
            } else {
                Admin admin = adminDao.selectByPhone(phone);
                if (admin == null) {
                    Admin a = new Admin();
                    a.setAdminName(adminName);
                    a.setAdminPass(adminPass);
                    a.setColumn5gmtModified(new Date());
                    a.setGmtCreate(new Date());
                    a.setField2(phone);
                    a.setField1((long) 1);
                    a.setFlag((long)0);
                    adminDao.insertSelective(a);
                    return gson
                            .toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.SUCCESS.getState(), "注册成功"));

                } else {
                    return gson
                            .toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.FAIL.getState(), "该手机号已被注册"));
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return gson.toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.INNER_ERROR.getState(), "网络异常"));
        }
    }

    @Override
    public String loginCount(String json) {
        try {
            Map<String, String> jsonMap = gson.fromJson(json, HashMap.class);
            String type = jsonMap.get("type").equals("")?"3":jsonMap.get("type");
            String adminName = jsonMap.get("adminName").equals("")?"3":jsonMap.get("adminName");
            List<AdminSentence> sList = new ArrayList<>();
            //如果是3不对该条件加限制
            sList = asDao.selectAllByNameOrType(type,adminName);
            return gson.toJson(new ResponseDate<List<AdminSentence>>(HttpEnum.OK.getState(), StateEnum.SUCCESS.getState(), "查询成功", sList));

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return gson.toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.INNER_ERROR.getState(), "网络异常"));
        }
    }


    @Override
    public String selectSentence(String json) {
        try {
            Map<String, String> jsonMap = gson.fromJson(json, HashMap.class);
            String type = jsonMap.get("type");
            List<sentence> sList = new ArrayList<>();
            sList = sDao.selectAllByType(type);
            return gson.toJson(new ResponseDate<List<sentence>>(HttpEnum.OK.getState(), StateEnum.SUCCESS.getState(), "查询成功", sList));

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return gson.toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.INNER_ERROR.getState(), "网络异常"));
        }
    }


    @Override
    public String insertSentence(String json) {
        try {
            Map<String, String> jsonMap = gson.fromJson(json, HashMap.class);
            String type = jsonMap.get("type");//吐槽类型
            String text = jsonMap.get("text");//吐槽内容
            String userId = jsonMap.get("userId");//吐槽人id
            String userName = jsonMap.get("userName");//吐槽人姓名
            
            
            sentence s1 = sDao.selectByText("text");
            //如果没有这句话，加入到吐槽表中
            if(s1==null) {
                s1 = new sentence();
                s1.setGmtCreate(new Date());
                s1.setGmtModified(new Date());
                s1.setFlag("0");
                s1.setSentenceText(text);
                s1.setSentenceType(type);
                sDao.insertSelective(s1);
            }
            //将记录添加到人和吐槽的关联表中
            AdminSentence as = new AdminSentence();
            as.setSentenceId(s1.getSentenceId());
            as.setSentenceType(type);
            as.setUserId(Integer.parseInt(userId));
            as.setUserName(userName);
            as.setAdminSentenceId(new Date().getTime());
            asDao.insertSelective(as);
            
            return gson.toJson(new ResponseDate(HttpEnum.OK.getState(), StateEnum.SUCCESS.getState(), "吐槽成功"));

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return gson.toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.INNER_ERROR.getState(), "网络异常"));
        }
    }


    @Override
    public String selectAllSentence() {
        try {
            List<sentence> sList = new ArrayList<>();
            sList = sDao.selectAll();
            return gson.toJson(new ResponseDate<List<sentence>>(HttpEnum.OK.getState(), StateEnum.SUCCESS.getState(), "查询成功", sList));

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return gson.toJson(new ResponseDate<>(HttpEnum.OK.getState(), StateEnum.INNER_ERROR.getState(), "网络异常"));
        }
    }




   
}